package Duoxiancheng;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Thread123_3 {
    static ExecutorService executors = Executors.newFixedThreadPool(10);
    static AtomicInteger num = new AtomicInteger(0);

    public static void main(String[] args) {
        new Thread(new MineThread(0,"1")).start();
        new Thread(new MineThread(1,"2")).start();
        new Thread(new MineThread(2,"3")).start();

    }

    static class MineThread implements Runnable{
        int state;
        String str;
        MineThread(int state,String str){
            this.state = state;
            this.str = str;
        }

        @Override
        public void run() {
            try{
                for (int i=0;i<10;i++){
                    synchronized (num){
                        while (num.get()%3!=state)
                            num.wait();
                        System.out.println(Thread.currentThread().getName()+":"+str);
                        num.incrementAndGet();
                        num.notifyAll();
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}
